﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PasswordManager
{
   partial class frmPasswordManager : Form
   {
      protected KeyPairGroupList m_sMainGroupList;
      protected DataSaveLoadInterface m_sDataSaveLoad;
      public frmPasswordManager()
      {
         InitializeComponent();
         m_sMainGroupList = new KeyPairGroupList();
         m_sDataSaveLoad = new DataSaveLoadInterface();
      }

      private void loadToolStripMenuItem_Click(object sender, EventArgs e)
      {
         DialogResult sDlgRes = dlgOpenDataFile.ShowDialog();
         if (sDlgRes == System.Windows.Forms.DialogResult.OK)
         {
            m_sDataSaveLoad.LoadFromFile(dlgOpenDataFile.FileName, m_sMainGroupList);
         }
      }

      private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
      {
         DialogResult sDlgRes = dlgSaveDataFile.ShowDialog();
         if (sDlgRes == System.Windows.Forms.DialogResult.OK)
         {
            m_sDataSaveLoad.SaveToFile(dlgSaveDataFile.FileName, true, m_sMainGroupList);
         }
      }

      private void closeToolStripMenuItem_Click(object sender, EventArgs e)
      {
         // TODO: check if unsaved and prompt to save... 
         m_sDataSaveLoad.ClearLastFile();
         m_sMainGroupList.Clear();
      }

      private void saveToolStripMenuItem_Click(object sender, EventArgs e)
      {
         if (System.IO.File.Exists(m_sDataSaveLoad.GetLastFile()) == true)
         {
            m_sDataSaveLoad.SaveToFile(m_sDataSaveLoad.GetLastFile(), true, m_sMainGroupList);
         }
         else
         {
            saveAsToolStripMenuItem_Click(sender, e);
         }
      }

      private void newToolStripMenuItem_Click(object sender, EventArgs e)
      {
         closeToolStripMenuItem_Click(sender, e);
      }

      private void exitToolStripMenuItem_Click(object sender, EventArgs e)
      {
         closeToolStripMenuItem_Click(sender, e);
         Application.Exit();
      }

      private void newGroupToolStripMenuItem_Click(object sender, EventArgs e)
      {
         dlgCreateGroup sDlg = new dlgCreateGroup();
         DialogResult sRes = sDlg.ShowDialog();
         if (sRes == System.Windows.Forms.DialogResult.OK)
         {
            string strGroup = sDlg.GroupName;
            if (m_sMainGroupList.NewGroup(strGroup) == true)
            {
               TreeNode sNode = new TreeNode();
               sNode.Text = strGroup;
               sNode.ContextMenuStrip = cxtmnuModifyGroup;
               treeListOfPasswords.Nodes.Add(sNode);
               treeListOfPasswords.Sort();
            }
         }
      }

      private void deleteGroupToolStripMenuItem_Click(object sender, EventArgs e)
      {
         if (treeListOfPasswords.SelectedNode != null)
         {
            if (m_sMainGroupList.RemoveGroup(treeListOfPasswords.SelectedNode.Text) == true)
            {
               treeListOfPasswords.Nodes.Remove(treeListOfPasswords.SelectedNode);
               treeListOfPasswords.Sort();
            }
         }
      }

      private void newKeyValuePairToolStripMenuItem_Click(object sender, EventArgs e)
      {
         if (treeListOfPasswords.SelectedNode != null)
         {
            TreeNode sSelected = treeListOfPasswords.SelectedNode;
            dlgNewKeyValuePair sDlg = new dlgNewKeyValuePair();
            DialogResult sRes = sDlg.ShowDialog();
            if (sRes == System.Windows.Forms.DialogResult.OK)
            {
               if (m_sMainGroupList.AddTo(sSelected.Text, sDlg.KeyName, sDlg.Password) == true)
               {
                  TreeNode sKeyNode = new TreeNode();
                  sKeyNode.Text = sDlg.KeyName;
                  sKeyNode.ContextMenuStrip = cxtmnuModifyKey;
                  TreeNode sValueNode = new TreeNode();
                  sValueNode.Text = sDlg.Password;
                  sValueNode.ContextMenuStrip = cxtmnuModifyValue;
                  sKeyNode.Nodes.Add(sValueNode);
                  sSelected.Nodes.Add(sKeyNode);
               }
            }
         }
      }
   }
}
